package com.sls.game.client;

import java.util.Date;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.game.pojo.CardExportLog;
import com.sls.game.pojo.CardInformation;

public class MinaClientHandler extends IoHandlerAdapter
{
	
	private static Logger log = Logger
			.getLogger(MinaClientHandler.class);
	@Override
	public void messageReceived( IoSession session, Object message ) throws Exception
	{
		String s = ( String ) message;
		log.info( "来自服务器的消息:" + s );
		// session.write(s);
	}

	@Override
	public void sessionClosed( IoSession session ) throws Exception
	{
		// log.info("服务器走了~");
	}

	@Override
	public void sessionCreated( IoSession session ) throws Exception
	{
		log.info( "one Client Connection" + session.getRemoteAddress() );
		// session.write( "我来了······" );
	}

	@Override
	public void sessionOpened( IoSession session ) throws Exception
	{
		super.sessionOpened( session );
		
		CardInformation info = new CardInformation();
		info.setCardId( "7771" );
//		info.setCardName( "动感地带" );
//		info.setCardPassword( "123456" );
//		info.setCardSerial( "8882" );
//		info.setCardType( 1 );
//		info.setCardCost( 20000 );
//		info.setCardPrice( 30000 );
//		info.setCardEffectTime( new Date() );
//		info.setCardImportBatchNum( "20120824a" );
//		info.setCardExportBatchNum( "20120824b" );
//		info.setAdaptableArea( "浙江" );
//		info.setArsCategory( 1 );
//		info.setGoodsId( "20120824175623" );
//		info.setOrderId( "20120824175623abcd" );
//		info.setSupplyCategory( 1 );
		info.setSaleState( 1 );
//		info.setSaleTime( new Date() );
//		info.setRemark( "备注" );
		
//		CardImportLog logInfo = new CardImportLog();
//		logInfo.setCardImportTime( new Date() );
//		logInfo.setImportCardNumber( 1 );
//		logInfo.setImportOperator( "chenxiang" );
//		info.setCardImportLog( logInfo );
		
		CardExportLog logInfo = new CardExportLog();
		logInfo.setCardExportTime( new Date() );
		logInfo.setExportCardNumber( 1 );
		logInfo.setExportCardBatchNum( "20120830155130a" );
		logInfo.setExportOperator( "小明" );
		logInfo.setExportState( 1 );
		info.setCardExportLog( logInfo );
		
		Gson gson = CommonUtil.gson();
		SocketDTO socketChangeVo = new SocketDTO();
		socketChangeVo.setBody( gson.toJson( info ) );
		Head head = new Head();
		head.setIp( "127.0.0.1" );
		head.setServiceCode( "005" );
		head.setUserId( "0" );
		head.setRequestId( UUID.randomUUID().toString() );
		socketChangeVo.setHead( head );
		String cardMsg = gson.toJson( socketChangeVo );
		session.write( cardMsg );
		log.info( cardMsg );
		log.info( "sesson open!!" );
		// session.close( true );
	}
}
